<?php

App::uses('AppHelper', 'View/Helper');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 * @author Duonght <hduongdmit@gmail.com>
 */
class CommonHelper extends AppHelper {
	
	/**
	 * Seo title
	 * 
	 * @param string $string
	 * @return string
	 */
	public function seoTitle($string) {
		$string = self::stripUnicode($string);
		$string = preg_replace("`\[.*\]`U","",$string);
		$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
		$string = htmlentities($string, ENT_COMPAT, 'utf-8');
		$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
		$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
		$string = $string.'.html';
		return strtolower(trim($string, '-'));
	}

	/**
	 * Get datetime 
	 * 
	 * @param string $language
	 * @param number $getDay
	 * @param number $getDate
	 * @param number $getTime
	 * @param string $timeZone
	 * @param number $intTimestamp
	 * @return string
	 */
	public function getDateTime($language = "vn", $getDay = 1, $getDate = 1, $getTime = 1, $timeZone = "GMT+7", $intTimestamp = 0){
		if($intTimestamp > 0){
			$today = getdate($intTimestamp);
			$day   = $today["wday"];
			$date  = date("d/m/Y", $intTimestamp);
			$time  = date("H:i", $intTimestamp);
		} else {
			$today = getdate();
			$day   = $today["wday"];
			$date  = date("d/m/Y", time());
			$time  = date("h:i");
		}

		switch ($language){
			case "vn": $dayArray = array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy"); break;
			case "en": $dayArray = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); break;
			default : $dayArray = array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy"); break;
		}

		$strDateTime = "";
		for ($i = 0; $i <= 6; $i ++){
			if($i == $day){
				if($getDay  != 0)	$strDateTime .= $dayArray[$i] . ", ";
				if($getDate != 0)	$strDateTime .= $date . " | ";
				if($getTime != 0)	$strDateTime .= $time . " ";
				$strDateTime .= $timeZone;
				if(substr($strDateTime, -2, 2) == ", ") $strDateTime = substr($strDateTime, 0, -2);

				return $strDateTime;
			}
		}
	}

	/**
	 * Convert datetime
	 * 
	 * @param string $strDate
	 * @param string $strTime
	 * @return Ambigous <>|number
	 */
	public function convertDateTime($strDate = "", $strTime = ""){
		//Break string and create array date time
		$strDate        = str_replace("/", "-", $strDate);
		$strDateArray   = explode("-", $strDate);
		$countDateArr   = count($strDateArray);
		$strTime        = str_replace("-", ":", $strTime);
		$strTimeArray   = explode(":", $strTime);
		$countTimeArr   = count($strTimeArray);

		//Get Current date time
		$today = getdate();
		$day   = $today["mday"];
		$mon   = $today["mon"];
		$year  = $today["year"];
		$hour  = $today["hours"];
		$min   = $today["minutes"];
		$sec   = $today["seconds"];

		//Get date array
		switch($countDateArr){
			case 2:
				$day = intval($strDateArray[0]);
				$mon = intval($strDateArray[1]);
				break;
			case $countDateArr >= 3:
				$day = intval($strDateArray[0]);
				$mon = intval($strDateArray[1]);
				$year = intval($strDateArray[2]);
				break;
		}

		//Get time array
		switch($countTimeArr){
			case 2:
				$hour = intval($strTimeArray[0]);
				$min = intval($strTimeArray[1]);
				break;
			case $countTimeArr >= 3:
				$hour = intval($strTimeArray[0]);
				$min = intval($strTimeArray[1]);
				$sec = intval($strTimeArray[2]);
				break;
		}

		//Return date time integer
		if(@mktime($hour, $min, $sec, $mon, $day, $year) == -1) return $today[0];
		else return mktime($hour, $min, $sec, $mon, $day, $year);
	}

	/**
	 * 
	 * @param unknown $day
	 * @param unknown $month
	 * @param unknown $year
	 * @param number $hour
	 * @param number $min
	 * @param number $second
	 * @return number
	 */
	public function dateToInt($day, $month, $year, $hour = 0, $min = 0, $second = 0){
		return mktime($hour, $min, $second, $month, $day, $year);
	}


	/**
	 * Strip unicode
	 * 
	 * @param string $str
	 * @return boolean|mixed
	 */
	static function stripUnicode($str){
		if(!$str) return false;
		$unicode = array(
				'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
				'd'=>'đ|Đ',
				'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
				'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
				'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
				'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
				'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
		);
		foreach($unicode as $nonUnicode=>$uni) $str = preg_replace("/($uni)/i",$nonUnicode,$str);
		return $str;
	}

}
